﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

/* 主窗体控件不遮盖子窗体 */
using System.Runtime.InteropServices; 


namespace RehabilitationRobot
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
            #region 窗体启动时计算康复模式数据
            Recovery.NormalStep();
            Recovery.SmallArc();
            Recovery.Circular();
            Recovery.Linear();
            Recovery.Oval();
            Recovery.Hip();
            Recovery.Knee();
            #endregion           

            #region 窗体启动时状态管理
            UIThread.ChangeEnable(btn_ConnectController, true);//连接运动控制器
            UIThread.ChangeEnable(btn_DisconnectController, false);//断开连接
            UIThread.ChangeEnable(btn_ServoOn, false);//伺服使能
            UIThread.ChangeEnable(btn_ServoOff, false);//伺服禁能
            UIThread.ChangeEnable(btn_EleActuatorUp, false);//电动推杆向上
            UIThread.ChangeEnable(btn_EleActuatorDown, false);//电动推杆向下    
            UIThread.ChangeEnable(btn_JogForm, false);//点动模块
            UIThread.ChangeEnable(btn_NormalStep, false);//标准步态训练模式
            UIThread.ChangeEnable(btn_SmallArc, false);//小圆弧训练模式
            UIThread.ChangeEnable(btn_Linear, false);//直线训练模式
            UIThread.ChangeEnable(btn_Oval, false);//椭圆训练模式
            UIThread.ChangeEnable(btn_Circular, false);//圆周训练模式
            UIThread.ChangeEnable(btn_Hip, false);//髋关节训练模式
            UIThread.ChangeEnable(btn_Knee, false);//膝关节训练模式
            UIThread.ChangeEnable(btn_StopTrain, false);//停止训练
            UIThread.ChangeEnable(btn_AllToZero, false);//全部回零
            #endregion
        }

        #region 全局变量定义区
        /* 单边/双边选择系数，默认双腿运行 */
        int ModeSelect = 0;

        /* 全局时间设置 */
        DateTime startTime;
        #endregion       
       
        #region 实例化对象区
        /* 实例化运动控制器对象 */
        public static MotionController MotionController = new MotionController();

        public JogForm JogForm = new JogForm();
        public RecoveryModule Recovery = new RecoveryModule();
        #endregion

        #region 线程规划区
        Thread Statethread;
        Thread MotionDownloadThread;
        #endregion

        #region  点动模块
        static bool m_jogformshow = false;
        static public bool JogFormShow
        {
            get { return m_jogformshow; }
            set { m_jogformshow = value; }
        }

        /* 主窗体控件不遮盖子窗体 */
        /* https://blog.csdn.net/chenyblog/article/details/40752029 */
        [DllImport("user32")]
        public static extern int SetParent(int hWndChild, int hWndNewParent);

        /* 点动模块按钮 */
        private void btn_JogForm_Click(object sender, EventArgs e)
        {
            if (!m_jogformshow)
            {          
                /* 将主窗体设置为父窗体 */
                JogForm.MdiParent = this;
                /* 以非模态对话框形式弹出 */
                JogForm.Show();

                /* 主窗体控件不遮盖子窗体 */
                SetParent((int)JogForm.Handle, (int)this.Handle);
                m_jogformshow = true;
            }
        }
        #endregion

        #region 版本管理窗体
        static bool m_releaseformshow = false;
        static public bool ReleaseFormShow
        {
            get { return m_releaseformshow; }
            set { m_releaseformshow = value; }
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!m_releaseformshow)
            {
                ReleaseManagement ReleaseManagement = new ReleaseManagement();
                ReleaseManagement.Show();
                m_releaseformshow = true;
            }

        }
        #endregion
       
        #region 状态管理线程托管函数
        private void StateManageThread()
        {
            while(true)
            {
                MotionController.CheckServoOn();
                MotionController.BitParameterRead();
                MotionController.AxisGroupStateRead();
                MotionController.CheckDFI();
                MotionController.CheckLimitSwitch();
                MotionController.CheckSoftwareLimit();
                MotionController.AngleRead();
                MotionController.FollowErrrRead();
                MotionController.MoveBufferRead();
                //MotionController.ParkerProgram2Start(); //限位开关Program2
                UIexchange();                
            }
            
        }

        /// <summary>
        /// UI线程数据交互
        /// </summary>
        private void UIexchange()
        {                       
            if (this.WindowState == FormWindowState.Normal)
            {
                /* 数据发送给Unity3d */
                Unity3D.PackAngleSendToUnity(u3Dplayer1, Unity3D.DoubArrayToString(MotionController.m_dRealTimeAngle));

                /* UI界面实时显示关节角度 */
                UIThread.ChangeText(textBox_HL, MotionController.m_dRealTimeAngle[0].ToString("F4"));
                UIThread.ChangeText(textBox_HR, MotionController.m_dRealTimeAngle[1].ToString("F4"));
                UIThread.ChangeText(textBox_KL, MotionController.m_dRealTimeAngle[2].ToString("F4"));
                UIThread.ChangeText(textBox_KR, MotionController.m_dRealTimeAngle[3].ToString("F4"));

                /* 点动模块实时显示关节角度 */
                if (!(JogForm == null))
                {
                    UIThread.ChangeText(JogForm.textBox_HLjog, MotionController.m_dRealTimeAngle[0].ToString("F4"));
                    UIThread.ChangeText(JogForm.textBox_HRjog, MotionController.m_dRealTimeAngle[1].ToString("F4"));
                    UIThread.ChangeText(JogForm.textBox_KLjog, MotionController.m_dRealTimeAngle[2].ToString("F4"));
                    UIThread.ChangeText(JogForm.textBox_KRjog, MotionController.m_dRealTimeAngle[3].ToString("F4"));
                }

                /* 选择单边/双边训练模式 */
                ModeSelect = UIThread.TrainModeSelect(radioButton1, radioButton2, radioButton3);
            }                          
        }

        #endregion

        /// <summary>
        /// 连接控制器
        /// </summary>
        /// <param name="sender">“连接控制器”控件</param>
        /// <param name="e">消息</param>
        private void btn_ConnectController_Click(object sender, EventArgs e)
        {
            try
            {
                MotionController.ConnectController();
                /* 检测当前连接控制器的状态 */
                MotionController.CheckConnectControllerStatus();

                /* 连接控制器成功 */
                if (MotionController.Controller_State.Controller_Connect)
                {
                    MessageBox.Show("连接控制器成功");
                    Statethread = new Thread(new ThreadStart(StateManageThread));
                    Statethread.Start();

                    #region 按键状态管理
                    UIThread.ChangeEnable(btn_ConnectController, false);//连接运动控制器
                    UIThread.ChangeEnable(btn_DisconnectController, true);//断开连接
                    UIThread.ChangeEnable(btn_ServoOn, true);//伺服使能  
                    UIThread.ChangeEnable(btn_EleActuatorUp, true);//电动推杆向上
                    UIThread.ChangeEnable(btn_EleActuatorDown, true);//电动推杆向下 
                    #endregion
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 断开连接 
        /// </summary>
        /// <param name="sender">“断开连接”控件</param>
        /// <param name="e">消息</param>
        private void btn_DisconnectController_Click(object sender, EventArgs e)
        {
            Statethread.Abort();
            MotionController.DisconnectController();
            /* 检查连接状态 */
            MotionController.CheckConnectControllerStatus();
            if (!MotionController.Controller_State.Controller_Connect)
            {
                MessageBox.Show("断开控制器成功");

                #region 按键状态管理
                UIThread.ChangeEnable(btn_ConnectController, true);//连接运动控制器
                UIThread.ChangeEnable(btn_DisconnectController, false);//断开连接
                UIThread.ChangeEnable(btn_ServoOn, false);//伺服使能 
                UIThread.ChangeEnable(btn_EleActuatorUp, false);//电动推杆向上
                UIThread.ChangeEnable(btn_EleActuatorDown, false);//电动推杆向下 
                #endregion
            }
            else
            {
                MessageBox.Show("断开控制器失败");
            }
        }

        /// <summary>
        /// 伺服使能
        /// </summary>
        /// <param name="sender">“伺服使能”按钮</param>
        /// <param name="e">消息</param>
        private void btn_ServoOn_Click(object sender, EventArgs e)
        {
            MotionController.ServoOn();
            MotionController.CheckServoOn();
            if (MotionController.Controller_State.Controller_AllMotion_DriveOn == true)
            {
                MessageBox.Show("使能成功");

                #region 按键状态管理
                UIThread.ChangeEnable(btn_DisconnectController, false);//断开连接
                UIThread.ChangeEnable(btn_ServoOn, false);//伺服使能
                UIThread.ChangeEnable(btn_ServoOff, true);//伺服禁能
                UIThread.ChangeEnable(btn_JogForm, true);//点动模块
                UIThread.ChangeEnable(btn_NormalStep, true);//标准步态训练模式
                UIThread.ChangeEnable(btn_SmallArc, true);//小圆弧训练模式
                UIThread.ChangeEnable(btn_Linear, true);//直线训练模式
                UIThread.ChangeEnable(btn_Oval, true);//椭圆训练模式
                UIThread.ChangeEnable(btn_Circular, true);//圆周训练模式
                UIThread.ChangeEnable(btn_Hip, true);//髋关节训练模式
                UIThread.ChangeEnable(btn_Knee, true);//膝关节训练模式               
                #endregion
            }  
            else
            {
                MessageBox.Show("使能失败");
            }
        }

        /// <summary>
        ///伺服禁能
        /// </summary>
        /// <param name="sender">“伺服禁能”按钮</param>
        /// <param name="e">消息</param>
        private void btn_ServoOff_Click(object sender, EventArgs e)
        {
            MotionController.ServoOff();
            MotionController.CheckServoOn();
            if (MotionController.Controller_State.Controller_AllMotion_DriveOn == false)
            {
                MessageBox.Show("禁能成功");

                #region 按键状态管理
                UIThread.ChangeEnable(btn_DisconnectController, true);//断开连接
                UIThread.ChangeEnable(btn_ServoOn, true);//伺服使能
                UIThread.ChangeEnable(btn_ServoOff, false);//伺服禁能
                UIThread.ChangeEnable(btn_JogForm, false);//点动模块
                UIThread.ChangeEnable(btn_NormalStep, false);//标准步态训练模式
                UIThread.ChangeEnable(btn_SmallArc, false);//小圆弧训练模式
                UIThread.ChangeEnable(btn_Linear, false);//直线训练模式
                UIThread.ChangeEnable(btn_Oval, false);//椭圆训练模式
                UIThread.ChangeEnable(btn_Circular, false);//圆周训练模式
                UIThread.ChangeEnable(btn_Hip, false);//髋关节训练模式
                UIThread.ChangeEnable(btn_Knee, false);//膝关节训练模式          
                #endregion
            } 
            else
            {
                MessageBox.Show("禁能失败");
            }
        }
        
        #region 标准步态
        private void btn_NormalStep_Click(object sender, EventArgs e)
        {
            MotionController.RobotMoveAllow();

            #region 训练计时
            startTime = DateTime.Now;
            timer_TrainTime.Enabled = true;
            timer_TrainTime.Interval = 500;
            timer_TrainTime.Start();
            #endregion
            
            #region 按键状态管理
            UIThread.ChangeEnable(btn_ServoOff, false);//伺服禁能
            UIThread.ChangeEnable(btn_JogForm, false);//点动模块
            UIThread.ChangeEnable(btn_EleActuatorUp, false);//电动推杆向上
            UIThread.ChangeEnable(btn_EleActuatorDown, false);//电动推杆向下 
            UIThread.ChangeEnable(btn_NormalStep, false);//标准步态训练模式
            UIThread.ChangeEnable(btn_SmallArc, false);//小圆弧训练模式
            UIThread.ChangeEnable(btn_Linear, false);//直线训练模式
            UIThread.ChangeEnable(btn_Oval, false);//椭圆训练模式
            UIThread.ChangeEnable(btn_Circular, false);//圆周训练模式
            UIThread.ChangeEnable(btn_Hip, false);//髋关节训练模式
            UIThread.ChangeEnable(btn_Knee, false);//膝关节训练模式
            UIThread.ChangeEnable(btn_StopTrain, true);//停止训练
            UIThread.ChangeEnable(radioButton1, false);//单边/双边模式选择
            UIThread.ChangeEnable(radioButton2, false);
            UIThread.ChangeEnable(radioButton3, false);
            #endregion

            for (int i = 0; i < 200; i++)
            {              
                double RecMode_Rov = UIThread.ReadTrackBarValue(trackBar_RecVel);
                double[] m_dMoveStaAngDownload = new double[4];//定义临时数组，用于存储下发角度值，并赋值               
                #region 单边训练模式选择
                switch (ModeSelect)
                {
                        /* 双边 */
                    case 0:
                        m_dMoveStaAngDownload[0] = Recovery.NormalStepStartData[0, i];
                        m_dMoveStaAngDownload[1] = Recovery.NormalStepStartData[1, i];
                        m_dMoveStaAngDownload[2] = Recovery.NormalStepStartData[2, i];
                        m_dMoveStaAngDownload[3] = Recovery.NormalStepStartData[3, i];
                        MotionController.RobotMoveDownload(m_dMoveStaAngDownload, RecMode_Rov * Recovery.NormalStepStartVel[i]);
                        break;
                        /* 左单边 */
                    case 1:
                        m_dMoveStaAngDownload[0] = Recovery.NormalStepStartData[0, i];
                        m_dMoveStaAngDownload[1] = 0;
                        m_dMoveStaAngDownload[2] = Recovery.NormalStepStartData[2, i];
                        m_dMoveStaAngDownload[3] = 0;
                        MotionController.RobotMoveDownload(m_dMoveStaAngDownload, RecMode_Rov * Recovery.NormalStepStartVel[i] / 2);
                        break;
                        /* 右单边 */
                    case 2:
                        m_dMoveStaAngDownload[0] = 0;
                        m_dMoveStaAngDownload[1] = Recovery.NormalStepStartData[1, i];
                        m_dMoveStaAngDownload[2] = 0;
                        m_dMoveStaAngDownload[3] = Recovery.NormalStepStartData[3, i];
                        MotionController.RobotMoveDownload(m_dMoveStaAngDownload, RecMode_Rov * Recovery.NormalStepStartVel[i] / 2);
                        break;
                }                             
                #endregion              
            }
            MotionDownloadThread = new Thread(new ThreadStart(NormalStepPeriodMotion));
            MotionDownloadThread.Start();
        }

        private void NormalStepPeriodMotion()
        {
            MotionController.RobotMoveAllow();
            while (true)
            {
                if (MotionController.m_iMoveBuffers < 50)
                {
                    for (int count = 0; count < 150; count++)
                    {
                        /* 获取速度等级 */
                        double RecMode_Rov = UIThread.ReadTrackBarValue(trackBar_RecVel);

                        double[] m_dMoveStdAngDownload = new double[4];
                        #region 单边训练模式选择
                        switch (ModeSelect)
                        {
                                /* 双腿（默认） */
                            case 0:
                                {
                                    m_dMoveStdAngDownload[0] = Recovery.NormalStepPeriodData[0, count];
                                    m_dMoveStdAngDownload[1] = Recovery.NormalStepPeriodData[1, count];
                                    m_dMoveStdAngDownload[2] = Recovery.NormalStepPeriodData[2, count];
                                    m_dMoveStdAngDownload[3] = Recovery.NormalStepPeriodData[3, count];
                                    MotionController.RobotMoveDownload(m_dMoveStdAngDownload, RecMode_Rov * Recovery.NormalStepPeriodVel[count]);
                                }                               
                                break;
                                /* 左单边 */
                            case 1:
                                {
                                    m_dMoveStdAngDownload[0] = Recovery.NormalStepPeriodData[0, count];
                                    m_dMoveStdAngDownload[1] = 0;
                                    m_dMoveStdAngDownload[2] = Recovery.NormalStepPeriodData[2, count];
                                    m_dMoveStdAngDownload[3] = 0;
                                    MotionController.RobotMoveDownload(m_dMoveStdAngDownload, RecMode_Rov * Recovery.NormalStepPeriodVel[count] / 2);
                                }                               
                                break;
                                /* 右单边 */
                            case 2:
                                {
                                    m_dMoveStdAngDownload[0] = 0;
                                    m_dMoveStdAngDownload[1] = Recovery.NormalStepPeriodData[1, count];
                                    m_dMoveStdAngDownload[2] = 0;
                                    m_dMoveStdAngDownload[3] = Recovery.NormalStepPeriodData[3, count];
                                    MotionController.RobotMoveDownload(m_dMoveStdAngDownload, RecMode_Rov * Recovery.NormalStepPeriodVel[count] / 2);
                                }                               
                                break;
                        }
                        #endregion                       
                    }
                }
            }
        }
        #endregion
         
        #region 小圆弧训练模式
        private void btn_SmallArc_Click(object sender, EventArgs e)
        {
            MotionController.RobotMoveAllow();

            #region 按键状态管理
            UIThread.ChangeEnable(btn_ServoOff, false);//伺服禁能
            UIThread.ChangeEnable(btn_JogForm, false);//点动模块
            UIThread.ChangeEnable(btn_EleActuatorUp, false);//电动推杆向上
            UIThread.ChangeEnable(btn_EleActuatorDown, false);//电动推杆向下 
            UIThread.ChangeEnable(btn_NormalStep, false);//标准步态训练模式
            UIThread.ChangeEnable(btn_SmallArc, false);//小圆弧训练模式
            UIThread.ChangeEnable(btn_Linear, false);//直线训练模式
            UIThread.ChangeEnable(btn_Oval, false);//椭圆训练模式
            UIThread.ChangeEnable(btn_Circular, false);//圆周训练模式
            UIThread.ChangeEnable(btn_Hip, false);//髋关节训练模式
            UIThread.ChangeEnable(btn_Knee, false);//膝关节训练模式
            UIThread.ChangeEnable(btn_StopTrain, true);//停止训练
            UIThread.ChangeEnable(radioButton1, false);//单边/双边模式选择
            UIThread.ChangeEnable(radioButton2, false);
            UIThread.ChangeEnable(radioButton3, false);
            #endregion

            for (int i = 0; i < 75; i++)
            {              
                /* 获取速度等级 */
                double RecMode_Rov = UIThread.ReadTrackBarValue(trackBar_RecVel);

                double[] m_dMoveStaAngDownload = new double[4];//定义临时数组，用于存储下发角度值，并赋值
                #region 单边训练模式选择
                switch(ModeSelect)
                {
                        /* 双腿（默认） */
                    case 0:
                        {
                            m_dMoveStaAngDownload[0] = Recovery.SmallArcStartData[0, i];
                            m_dMoveStaAngDownload[1] = Recovery.SmallArcStartData[1, i];
                            m_dMoveStaAngDownload[2] = Recovery.SmallArcStartData[2, i];
                            m_dMoveStaAngDownload[3] = Recovery.SmallArcStartData[3, i];
                            MotionController.RobotMoveDownload(m_dMoveStaAngDownload, RecMode_Rov * Recovery.SmallArcStartVel[i]);
                        }
                        break;
                        /* 左单边 */
                    case 1:
                        {
                            m_dMoveStaAngDownload[0] = Recovery.SmallArcStartData[0, i];
                            m_dMoveStaAngDownload[1] = 0;
                            m_dMoveStaAngDownload[2] = Recovery.SmallArcStartData[2, i];
                            m_dMoveStaAngDownload[3] = 0;
                            MotionController.RobotMoveDownload(m_dMoveStaAngDownload, RecMode_Rov * Recovery.SmallArcStartVel[i] / 2);
                        }
                        break;
                        /* 右单边 */
                    case 2:
                        {
                            m_dMoveStaAngDownload[0] = 0;
                            m_dMoveStaAngDownload[1] = Recovery.SmallArcStartData[1, i];
                            m_dMoveStaAngDownload[2] = 0;
                            m_dMoveStaAngDownload[3] = Recovery.SmallArcStartData[3, i];
                            MotionController.RobotMoveDownload(m_dMoveStaAngDownload, RecMode_Rov * Recovery.SmallArcStartVel[i] / 2);
                        }
                        break;
                }
                #endregion               
            }
            MotionDownloadThread = new Thread(new ThreadStart(SmallArcPeriodMotion));
            MotionDownloadThread.Start();
        }

        private void SmallArcPeriodMotion()
        {
            MotionController.RobotMoveAllow();
            while (true)
            {
                if (MotionController.m_iMoveBuffers < 50)
                {
                    for (int count = 0; count < 150; count++)
                    {
                        /* 获取速度等级 */
                        double RecMode_Rov = UIThread.ReadTrackBarValue(trackBar_RecVel);

                        double[] m_dMoveStdAngDownload = new double[4];
                        #region 单边训练模式选择
                        switch(ModeSelect)
                        {
                                /* 双腿（默认） */
                            case 0:
                                {
                                    m_dMoveStdAngDownload[0] = Recovery.SmallArcPeriodData[0, count];
                                    m_dMoveStdAngDownload[1] = Recovery.SmallArcPeriodData[1, count];
                                    m_dMoveStdAngDownload[2] = Recovery.SmallArcPeriodData[2, count];
                                    m_dMoveStdAngDownload[3] = Recovery.SmallArcPeriodData[3, count];
                                    MotionController.RobotMoveDownload(m_dMoveStdAngDownload, RecMode_Rov * Recovery.SmallArcPeriodVel[count]);
                                }
                                break;
                                /* 左单边 */
                            case 1:
                                {
                                    m_dMoveStdAngDownload[0] = Recovery.SmallArcPeriodData[0, count];
                                    m_dMoveStdAngDownload[1] = 0;
                                    m_dMoveStdAngDownload[2] = Recovery.SmallArcPeriodData[2, count];
                                    m_dMoveStdAngDownload[3] = 0;
                                    MotionController.RobotMoveDownload(m_dMoveStdAngDownload, RecMode_Rov * Recovery.SmallArcPeriodVel[count] / 2);
                                }
                                break;
                                /* 右单边 */
                            case 2:
                                {
                                    m_dMoveStdAngDownload[0] = 0;
                                    m_dMoveStdAngDownload[1] = Recovery.SmallArcPeriodData[1, count];
                                    m_dMoveStdAngDownload[2] = 0;
                                    m_dMoveStdAngDownload[3] = Recovery.SmallArcPeriodData[3, count];
                                    MotionController.RobotMoveDownload(m_dMoveStdAngDownload, RecMode_Rov * Recovery.SmallArcPeriodVel[count] / 2);
                                }
                                break;
                        }
                        #endregion                       
                    }
                }
            }
        }
        #endregion                     

        #region 直线训练模式
        private void btn_Linear_Click(object sender, EventArgs e)
        {
            MotionController.RobotMoveAllow();

            #region 按键状态管理
            UIThread.ChangeEnable(btn_ServoOff, false);//伺服禁能
            UIThread.ChangeEnable(btn_JogForm, false);//点动模块
            UIThread.ChangeEnable(btn_EleActuatorUp, false);//电动推杆向上
            UIThread.ChangeEnable(btn_EleActuatorDown, false);//电动推杆向下 
            UIThread.ChangeEnable(btn_NormalStep, false);//标准步态训练模式
            UIThread.ChangeEnable(btn_SmallArc, false);//小圆弧训练模式
            UIThread.ChangeEnable(btn_Linear, false);//直线训练模式
            UIThread.ChangeEnable(btn_Oval, false);//椭圆训练模式
            UIThread.ChangeEnable(btn_Circular, false);//圆周训练模式
            UIThread.ChangeEnable(btn_Hip, false);//髋关节训练模式
            UIThread.ChangeEnable(btn_Knee, false);//膝关节训练模式
            UIThread.ChangeEnable(btn_StopTrain, true);//停止训练
            UIThread.ChangeEnable(radioButton1, false);//单边/双边模式选择
            UIThread.ChangeEnable(radioButton2, false);
            UIThread.ChangeEnable(radioButton3, false);
            #endregion

            for (int i = 0; i < 75; i++)
            {              
                /* 获取速度等级 */
                double RecMode_Rov = UIThread.ReadTrackBarValue(trackBar_RecVel);

                double[] m_dMoveStaAngDownload = new double[4];//定义临时数组，用于存储下发角度值，并赋值

                #region 单边训练模式选择
                switch (ModeSelect)
                {
                    /* 双边 */
                    case 0:
                        m_dMoveStaAngDownload[0] = Recovery.LinearStartData[0, i];
                        m_dMoveStaAngDownload[1] = Recovery.LinearStartData[1, i];
                        m_dMoveStaAngDownload[2] = Recovery.LinearStartData[2, i];
                        m_dMoveStaAngDownload[3] = Recovery.LinearStartData[3, i];
                        MotionController.RobotMoveDownload(m_dMoveStaAngDownload, RecMode_Rov * Recovery.LinearStartVel[i]);
                        break;
                    /* 左单边 */
                    case 1:
                        m_dMoveStaAngDownload[0] = Recovery.LinearStartData[0, i];
                        m_dMoveStaAngDownload[1] = 0;
                        m_dMoveStaAngDownload[2] = Recovery.LinearStartData[2, i];
                        m_dMoveStaAngDownload[3] = 0;
                        MotionController.RobotMoveDownload(m_dMoveStaAngDownload, RecMode_Rov * Recovery.LinearStartVel[i] / 2);
                        break;
                    /* 右单边 */
                    case 2:
                        m_dMoveStaAngDownload[0] = 0;
                        m_dMoveStaAngDownload[1] = Recovery.LinearStartData[1, i];
                        m_dMoveStaAngDownload[2] = 0;
                        m_dMoveStaAngDownload[3] = Recovery.LinearStartData[3, i];
                        MotionController.RobotMoveDownload(m_dMoveStaAngDownload, RecMode_Rov * Recovery.LinearStartVel[i] / 2);
                        break;
                }
                #endregion                
            }
            MotionDownloadThread = new Thread(new ThreadStart(LinearPeriodMotion));
            MotionDownloadThread.Start();
        }

        private void LinearPeriodMotion()
        {
            MotionController.RobotMoveAllow();
            while (true)
            {
                if (MotionController.m_iMoveBuffers < 50)
                {
                    for (int count = 0; count < 150; count++)
                    {
                        /* 获取速度等级 */
                        double RecMode_Rov = UIThread.ReadTrackBarValue(trackBar_RecVel);

                        double[] m_dMoveStdAngDownload = new double[4];
                        #region 单边训练模式选择
                        switch (ModeSelect)
                        {
                            /* 双腿（默认） */
                            case 0:
                                {
                                    m_dMoveStdAngDownload[0] = Recovery.LinearPeriodData[0, count];
                                    m_dMoveStdAngDownload[1] = Recovery.LinearPeriodData[1, count];
                                    m_dMoveStdAngDownload[2] = Recovery.LinearPeriodData[2, count];
                                    m_dMoveStdAngDownload[3] = Recovery.LinearPeriodData[3, count];
                                    MotionController.RobotMoveDownload(m_dMoveStdAngDownload, RecMode_Rov * Recovery.LinearPeriodVel[count]);
                                }
                                break;
                            /* 左单边 */
                            case 1:
                                {
                                    m_dMoveStdAngDownload[0] = Recovery.LinearPeriodData[0, count];
                                    m_dMoveStdAngDownload[1] = 0;
                                    m_dMoveStdAngDownload[2] = Recovery.LinearPeriodData[2, count];
                                    m_dMoveStdAngDownload[3] = 0;
                                    MotionController.RobotMoveDownload(m_dMoveStdAngDownload, RecMode_Rov * Recovery.LinearPeriodVel[count] / 2);
                                }
                                break;
                            /* 右单边 */
                            case 2:
                                {
                                    m_dMoveStdAngDownload[0] = 0;
                                    m_dMoveStdAngDownload[1] = Recovery.LinearPeriodData[1, count];
                                    m_dMoveStdAngDownload[2] = 0;
                                    m_dMoveStdAngDownload[3] = Recovery.LinearPeriodData[3, count];
                                    MotionController.RobotMoveDownload(m_dMoveStdAngDownload, RecMode_Rov * Recovery.LinearPeriodVel[count] / 2);
                                }
                                break;
                        }
                        #endregion                        
                    }
                }
            }
        }
        #endregion

        #region 圆周训练模式
        private void btn_Circular_Click(object sender, EventArgs e)
        {
            MotionController.RobotMoveAllow();
            for (int i = 0; i < 75; i++)
            {
                #region 按键状态管理
                UIThread.ChangeEnable(btn_ServoOff, false);//伺服禁能
                UIThread.ChangeEnable(btn_JogForm, false);//点动模块
                UIThread.ChangeEnable(btn_EleActuatorUp, false);//电动推杆向上
                UIThread.ChangeEnable(btn_EleActuatorDown, false);//电动推杆向下 
                UIThread.ChangeEnable(btn_NormalStep, false);//标准步态训练模式
                UIThread.ChangeEnable(btn_SmallArc, false);//小圆弧训练模式
                UIThread.ChangeEnable(btn_Linear, false);//直线训练模式
                UIThread.ChangeEnable(btn_Oval, false);//椭圆训练模式
                UIThread.ChangeEnable(btn_Circular, false);//圆周训练模式
                UIThread.ChangeEnable(btn_Hip, false);//髋关节训练模式
                UIThread.ChangeEnable(btn_Knee, false);//膝关节训练模式
                UIThread.ChangeEnable(btn_StopTrain, true);//停止训练
                UIThread.ChangeEnable(radioButton1, false);//单边/双边模式选择
                UIThread.ChangeEnable(radioButton2, false);
                UIThread.ChangeEnable(radioButton3, false);
                #endregion

                /* 获取速度等级 */
                double RecMode_Rov = UIThread.ReadTrackBarValue(trackBar_RecVel);

                double[] m_dMoveStaAngDownload = new double[4];//定义临时数组，用于存储下发角度值，并赋值
                #region 单边训练模式选择
                switch (ModeSelect)
                {
                    /* 双边 */
                    case 0:
                        m_dMoveStaAngDownload[0] = Recovery.CircularStartData[0, i];
                        m_dMoveStaAngDownload[1] = Recovery.CircularStartData[1, i];
                        m_dMoveStaAngDownload[2] = Recovery.CircularStartData[2, i];
                        m_dMoveStaAngDownload[3] = Recovery.CircularStartData[3, i];
                        MotionController.RobotMoveDownload(m_dMoveStaAngDownload, RecMode_Rov * Recovery.CircularStartVel[i]);
                        break;
                    /* 左单边 */
                    case 1:
                        m_dMoveStaAngDownload[0] = Recovery.CircularStartData[0, i];
                        m_dMoveStaAngDownload[1] = 0;
                        m_dMoveStaAngDownload[2] = Recovery.CircularStartData[2, i];
                        m_dMoveStaAngDownload[3] = 0;
                        MotionController.RobotMoveDownload(m_dMoveStaAngDownload, RecMode_Rov * Recovery.CircularStartVel[i] / 2);
                        break;
                    /* 右单边 */
                    case 2:
                        m_dMoveStaAngDownload[0] = 0;
                        m_dMoveStaAngDownload[1] = Recovery.CircularStartData[1, i];
                        m_dMoveStaAngDownload[2] = 0;
                        m_dMoveStaAngDownload[3] = Recovery.CircularStartData[3, i];
                        MotionController.RobotMoveDownload(m_dMoveStaAngDownload, RecMode_Rov * Recovery.CircularStartVel[i] / 2);
                        break;
                }
                #endregion                 
            }
            MotionDownloadThread = new Thread(new ThreadStart(CircularPeriodMotion));
            MotionDownloadThread.Start();
        }

        private void CircularPeriodMotion()
        {
            MotionController.RobotMoveAllow();
            while (true)
            {
                if (MotionController.m_iMoveBuffers < 50)
                {
                    for (int count = 0; count < 150; count++)
                    {
                        /* 获取速度等级 */
                        double RecMode_Rov = UIThread.ReadTrackBarValue(trackBar_RecVel);

                        double[] m_dMoveStdAngDownload = new double[4];
                        #region 单边训练模式选择
                        switch (ModeSelect)
                        {
                            /* 双腿（默认） */
                            case 0:
                                {
                                    m_dMoveStdAngDownload[0] = Recovery.CircularPeriodData[0, count];
                                    m_dMoveStdAngDownload[1] = Recovery.CircularPeriodData[1, count];
                                    m_dMoveStdAngDownload[2] = Recovery.CircularPeriodData[2, count];
                                    m_dMoveStdAngDownload[3] = Recovery.CircularPeriodData[3, count];
                                    MotionController.RobotMoveDownload(m_dMoveStdAngDownload, RecMode_Rov * Recovery.CircularPeriodVel[count]);
                                }
                                break;
                            /* 左单边 */
                            case 1:
                                {
                                    m_dMoveStdAngDownload[0] = Recovery.CircularPeriodData[0, count];
                                    m_dMoveStdAngDownload[1] = 0;
                                    m_dMoveStdAngDownload[2] = Recovery.CircularPeriodData[2, count];
                                    m_dMoveStdAngDownload[3] = 0;
                                    MotionController.RobotMoveDownload(m_dMoveStdAngDownload, RecMode_Rov * Recovery.CircularPeriodVel[count] / 2);
                                }
                                break;
                            /* 右单边 */
                            case 2:
                                {
                                    m_dMoveStdAngDownload[0] = 0;
                                    m_dMoveStdAngDownload[1] = Recovery.CircularPeriodData[1, count];
                                    m_dMoveStdAngDownload[2] = 0;
                                    m_dMoveStdAngDownload[3] = Recovery.CircularPeriodData[3, count];
                                    MotionController.RobotMoveDownload(m_dMoveStdAngDownload, RecMode_Rov * Recovery.CircularPeriodVel[count] / 2);
                                }
                                break;
                        }
                        #endregion 
                    }
                }
            }
        }
        #endregion      

        #region 椭圆训练模式
        private void btn_Oval_Click(object sender, EventArgs e)
        {
            MotionController.RobotMoveAllow();

            #region 按键状态管理
            UIThread.ChangeEnable(btn_ServoOff, false);//伺服禁能
            UIThread.ChangeEnable(btn_JogForm, false);//点动模块
            UIThread.ChangeEnable(btn_EleActuatorUp, false);//电动推杆向上
            UIThread.ChangeEnable(btn_EleActuatorDown, false);//电动推杆向下 
            UIThread.ChangeEnable(btn_NormalStep, false);//标准步态训练模式
            UIThread.ChangeEnable(btn_SmallArc, false);//小圆弧训练模式
            UIThread.ChangeEnable(btn_Linear, false);//直线训练模式
            UIThread.ChangeEnable(btn_Oval, false);//椭圆训练模式
            UIThread.ChangeEnable(btn_Circular, false);//圆周训练模式
            UIThread.ChangeEnable(btn_Hip, false);//髋关节训练模式
            UIThread.ChangeEnable(btn_Knee, false);//膝关节训练模式
            UIThread.ChangeEnable(btn_StopTrain, true);//停止训练
            UIThread.ChangeEnable(radioButton1, false);//单边/双边模式选择
            UIThread.ChangeEnable(radioButton2, false);
            UIThread.ChangeEnable(radioButton3, false);
            #endregion

            for (int i = 0; i < 75; i++)
            {               
                /* 获取速度等级 */
                double RecMode_Rov = UIThread.ReadTrackBarValue(trackBar_RecVel);

                double[] m_dMoveStaAngDownload = new double[4];//定义临时数组，用于存储下发角度值，并赋值
                #region 单边训练模式选择
                switch (ModeSelect)
                {
                    /* 双边 */
                    case 0:
                        m_dMoveStaAngDownload[0] = Recovery.OvalStartData[0, i];
                        m_dMoveStaAngDownload[1] = Recovery.OvalStartData[1, i];
                        m_dMoveStaAngDownload[2] = Recovery.OvalStartData[2, i];
                        m_dMoveStaAngDownload[3] = Recovery.OvalStartData[3, i];
                        MotionController.RobotMoveDownload(m_dMoveStaAngDownload, RecMode_Rov * Recovery.OvalStartVel[i]);
                        break;
                    /* 左单边 */
                    case 1:
                        m_dMoveStaAngDownload[0] = Recovery.OvalStartData[0, i];
                        m_dMoveStaAngDownload[1] = 0;
                        m_dMoveStaAngDownload[2] = Recovery.OvalStartData[2, i];
                        m_dMoveStaAngDownload[3] = 0;
                        MotionController.RobotMoveDownload(m_dMoveStaAngDownload, RecMode_Rov * Recovery.OvalStartVel[i] / 2);
                        break;
                    /* 右单边 */
                    case 2:
                        m_dMoveStaAngDownload[0] = 0;
                        m_dMoveStaAngDownload[1] = Recovery.OvalStartData[1, i];
                        m_dMoveStaAngDownload[2] = 0;
                        m_dMoveStaAngDownload[3] = Recovery.OvalStartData[3, i];
                        MotionController.RobotMoveDownload(m_dMoveStaAngDownload, RecMode_Rov * Recovery.OvalStartVel[i] / 2);
                        break;
                }
                #endregion                  
            }
            MotionDownloadThread = new Thread(new ThreadStart(OvalPeriodMotion));
            MotionDownloadThread.Start();
        }

        private void OvalPeriodMotion()
        {
            MotionController.RobotMoveAllow();
            while (true)
            {
                if (MotionController.m_iMoveBuffers < 50)
                {
                    for (int count = 0; count < 150; count++)
                    {
                        /* 获取速度等级 */
                        double RecMode_Rov = UIThread.ReadTrackBarValue(trackBar_RecVel);

                        double[] m_dMoveStdAngDownload = new double[4];
                        #region 单边训练模式选择
                        switch (ModeSelect)
                        {
                            /* 双腿（默认） */
                            case 0:
                                {
                                    m_dMoveStdAngDownload[0] = Recovery.OvalPeriodData[0, count];
                                    m_dMoveStdAngDownload[1] = Recovery.OvalPeriodData[1, count];
                                    m_dMoveStdAngDownload[2] = Recovery.OvalPeriodData[2, count];
                                    m_dMoveStdAngDownload[3] = Recovery.OvalPeriodData[3, count];
                                    MotionController.RobotMoveDownload(m_dMoveStdAngDownload, RecMode_Rov * Recovery.OvalPeriodVel[count]);
                                }
                                break;
                            /* 左单边 */
                            case 1:
                                {
                                    m_dMoveStdAngDownload[0] = Recovery.OvalPeriodData[0, count];
                                    m_dMoveStdAngDownload[1] = 0;
                                    m_dMoveStdAngDownload[2] = Recovery.OvalPeriodData[2, count];
                                    m_dMoveStdAngDownload[3] = 0;
                                    MotionController.RobotMoveDownload(m_dMoveStdAngDownload, RecMode_Rov * Recovery.OvalPeriodVel[count] / 2);
                                }
                                break;
                            /* 右单边 */
                            case 2:
                                {
                                    m_dMoveStdAngDownload[0] = 0;
                                    m_dMoveStdAngDownload[1] = Recovery.OvalPeriodData[1, count];
                                    m_dMoveStdAngDownload[2] = 0;
                                    m_dMoveStdAngDownload[3] = Recovery.OvalPeriodData[3, count];
                                    MotionController.RobotMoveDownload(m_dMoveStdAngDownload, RecMode_Rov * Recovery.OvalPeriodVel[count] / 2);
                                }
                                break;
                        }
                        #endregion                         
                    }
                }
            }
        }
        #endregion
        
        #region 髋关节训练模式
        private void btn_Hip_Click(object sender, EventArgs e)
        {
            MotionController.RobotMoveAllow();

            #region 按键状态管理
            UIThread.ChangeEnable(btn_ServoOff, false);//伺服禁能
            UIThread.ChangeEnable(btn_JogForm, false);//点动模块
            UIThread.ChangeEnable(btn_EleActuatorUp, false);//电动推杆向上
            UIThread.ChangeEnable(btn_EleActuatorDown, false);//电动推杆向下 
            UIThread.ChangeEnable(btn_NormalStep, false);//标准步态训练模式
            UIThread.ChangeEnable(btn_SmallArc, false);//小圆弧训练模式
            UIThread.ChangeEnable(btn_Linear, false);//直线训练模式
            UIThread.ChangeEnable(btn_Oval, false);//椭圆训练模式
            UIThread.ChangeEnable(btn_Circular, false);//圆周训练模式
            UIThread.ChangeEnable(btn_Hip, false);//髋关节训练模式
            UIThread.ChangeEnable(btn_Knee, false);//膝关节训练模式
            UIThread.ChangeEnable(btn_StopTrain, true);//停止训练
            UIThread.ChangeEnable(radioButton1, false);//单边/双边模式选择
            UIThread.ChangeEnable(radioButton2, false);
            UIThread.ChangeEnable(radioButton3, false);
            #endregion

            for (int i = 0; i < 37; i++)
            {
                /* 获取速度等级 */
                double RecMode_Rov = UIThread.ReadTrackBarValue(trackBar_RecVel);

                double[] m_dMoveStaAngDownload = new double[4];//定义临时数组，用于存储下发角度值，并赋值
                #region 单边训练模式选择
                switch (ModeSelect)
                {
                    /* 双边 */
                    case 0:
                        m_dMoveStaAngDownload[0] = Recovery.HipStartData[0, i];
                        m_dMoveStaAngDownload[1] = Recovery.HipStartData[1, i];
                        m_dMoveStaAngDownload[2] = Recovery.HipStartData[2, i];
                        m_dMoveStaAngDownload[3] = Recovery.HipStartData[3, i];
                        MotionController.RobotMoveDownload(m_dMoveStaAngDownload, RecMode_Rov * Recovery.HipStartVel[i]);
                        break;
                    /* 左单边 */
                    case 1:
                        m_dMoveStaAngDownload[0] = Recovery.HipStartData[0, i];
                        m_dMoveStaAngDownload[1] = 0;
                        m_dMoveStaAngDownload[2] = Recovery.HipStartData[2, i];
                        m_dMoveStaAngDownload[3] = 0;
                        MotionController.RobotMoveDownload(m_dMoveStaAngDownload, RecMode_Rov * Recovery.HipStartVel[i] / 2);
                        break;
                    /* 右单边 */
                    case 2:
                        m_dMoveStaAngDownload[0] = 0;
                        m_dMoveStaAngDownload[1] = Recovery.HipStartData[1, i];
                        m_dMoveStaAngDownload[2] = 0;
                        m_dMoveStaAngDownload[3] = Recovery.HipStartData[3, i];
                        MotionController.RobotMoveDownload(m_dMoveStaAngDownload, RecMode_Rov * Recovery.HipStartVel[i] / 2);
                        break;
                }
                #endregion                 
            }
            MotionDownloadThread = new Thread(new ThreadStart(HipPeriodMotion));
            MotionDownloadThread.Start();
        }

        private void HipPeriodMotion()
        {
            MotionController.RobotMoveAllow();
            while (true)
            {
                if (MotionController.m_iMoveBuffers < 50)
                {
                    for (int count = 0; count < 150; count++)
                    {
                        /* 获取速度等级 */
                        double RecMode_Rov = UIThread.ReadTrackBarValue(trackBar_RecVel);

                        double[] m_dMoveStdAngDownload = new double[4];
                        #region 单边训练模式选择
                        switch (ModeSelect)
                        {
                            /* 双腿（默认） */
                            case 0:
                                {
                                    m_dMoveStdAngDownload[0] = Recovery.HipPeriodData[0, count];
                                    m_dMoveStdAngDownload[1] = Recovery.HipPeriodData[1, count];
                                    m_dMoveStdAngDownload[2] = Recovery.HipPeriodData[2, count];
                                    m_dMoveStdAngDownload[3] = Recovery.HipPeriodData[3, count];
                                    MotionController.RobotMoveDownload(m_dMoveStdAngDownload, RecMode_Rov * Recovery.HipPeriodVel[count]);
                                }
                                break;
                            /* 左单边 */
                            case 1:
                                {
                                    m_dMoveStdAngDownload[0] = Recovery.HipPeriodData[0, count];
                                    m_dMoveStdAngDownload[1] = 0;
                                    m_dMoveStdAngDownload[2] = Recovery.HipPeriodData[2, count];
                                    m_dMoveStdAngDownload[3] = 0;
                                    MotionController.RobotMoveDownload(m_dMoveStdAngDownload, RecMode_Rov * Recovery.HipPeriodVel[count] / 2);
                                }
                                break;
                            /* 右单边 */
                            case 2:
                                {
                                    m_dMoveStdAngDownload[0] = 0;
                                    m_dMoveStdAngDownload[1] = Recovery.HipPeriodData[1, count];
                                    m_dMoveStdAngDownload[2] = 0;
                                    m_dMoveStdAngDownload[3] = Recovery.HipPeriodData[3, count];
                                    MotionController.RobotMoveDownload(m_dMoveStdAngDownload, RecMode_Rov * Recovery.HipPeriodVel[count] / 2);
                                }
                                break;
                        }
                        #endregion                           
                    }
                }
            }
        }
        #endregion
       
        #region 膝关节训练模式
        private void btn_Knee_Click(object sender, EventArgs e)
        {
            MotionController.RobotMoveAllow();

            #region 按键状态管理
            UIThread.ChangeEnable(btn_ServoOff, false);//伺服禁能
            UIThread.ChangeEnable(btn_JogForm, false);//点动模块
            UIThread.ChangeEnable(btn_EleActuatorUp, false);//电动推杆向上
            UIThread.ChangeEnable(btn_EleActuatorDown, false);//电动推杆向下 
            UIThread.ChangeEnable(btn_NormalStep, false);//标准步态训练模式
            UIThread.ChangeEnable(btn_SmallArc, false);//小圆弧训练模式
            UIThread.ChangeEnable(btn_Linear, false);//直线训练模式
            UIThread.ChangeEnable(btn_Oval, false);//椭圆训练模式
            UIThread.ChangeEnable(btn_Circular, false);//圆周训练模式
            UIThread.ChangeEnable(btn_Hip, false);//髋关节训练模式
            UIThread.ChangeEnable(btn_Knee, false);//膝关节训练模式
            UIThread.ChangeEnable(btn_StopTrain, true);//停止训练
            UIThread.ChangeEnable(radioButton1, false);//单边/双边模式选择
            UIThread.ChangeEnable(radioButton2, false);
            UIThread.ChangeEnable(radioButton3, false);
            #endregion

            for (int i = 0; i < 75; i++)
            {
                /* 获取速度等级 */
                double RecMode_Rov = UIThread.ReadTrackBarValue(trackBar_RecVel);

                double[] m_dMoveStaAngDownload = new double[4];//定义临时数组，用于存储下发角度值，并赋值
                #region 单边训练模式选择
                switch (ModeSelect)
                {
                    /* 双边 */
                    case 0:
                        m_dMoveStaAngDownload[0] = Recovery.KneeStartData[0, i];
                        m_dMoveStaAngDownload[1] = Recovery.KneeStartData[1, i];
                        m_dMoveStaAngDownload[2] = Recovery.KneeStartData[2, i];
                        m_dMoveStaAngDownload[3] = Recovery.KneeStartData[3, i];
                        MotionController.RobotMoveDownload(m_dMoveStaAngDownload, RecMode_Rov * Recovery.KneeStartVel[i]);
                        break;
                    /* 左单边 */
                    case 1:
                        m_dMoveStaAngDownload[0] = Recovery.KneeStartData[0, i];
                        m_dMoveStaAngDownload[1] = 0;
                        m_dMoveStaAngDownload[2] = Recovery.KneeStartData[2, i];
                        m_dMoveStaAngDownload[3] = 0;
                        MotionController.RobotMoveDownload(m_dMoveStaAngDownload, RecMode_Rov * Recovery.KneeStartVel[i] / 2);
                        break;
                    /* 右单边 */
                    case 2:
                        m_dMoveStaAngDownload[0] = 0;
                        m_dMoveStaAngDownload[1] = Recovery.KneeStartData[1, i];
                        m_dMoveStaAngDownload[2] = 0;
                        m_dMoveStaAngDownload[3] = Recovery.KneeStartData[3, i];
                        MotionController.RobotMoveDownload(m_dMoveStaAngDownload, RecMode_Rov * Recovery.KneeStartVel[i] / 2);
                        break;
                }
                #endregion                
            }
            MotionDownloadThread = new Thread(new ThreadStart(KneePeriodMotion));
            MotionDownloadThread.Start();
        }

        private void KneePeriodMotion()
        {
            MotionController.RobotMoveAllow();
            while (true)
            {
                if (MotionController.m_iMoveBuffers < 50)
                {
                    for (int count = 0; count < 150; count++)
                    {
                        /* 获取速度等级 */
                        double RecMode_Rov = UIThread.ReadTrackBarValue(trackBar_RecVel);

                        double[] m_dMoveStdAngDownload = new double[4];
                        #region 单边训练模式选择
                        switch (ModeSelect)
                        {
                            /* 双腿（默认） */
                            case 0:
                                {
                                    m_dMoveStdAngDownload[0] = Recovery.KneePeriodData[0, count];
                                    m_dMoveStdAngDownload[1] = Recovery.KneePeriodData[1, count];
                                    m_dMoveStdAngDownload[2] = Recovery.KneePeriodData[2, count];
                                    m_dMoveStdAngDownload[3] = Recovery.KneePeriodData[3, count];
                                    MotionController.RobotMoveDownload(m_dMoveStdAngDownload, RecMode_Rov * Recovery.KneePeriodVel[count]);
                                }
                                break;
                            /* 左单边 */
                            case 1:
                                {
                                    m_dMoveStdAngDownload[0] = Recovery.KneePeriodData[0, count];
                                    m_dMoveStdAngDownload[1] = 0;
                                    m_dMoveStdAngDownload[2] = Recovery.KneePeriodData[2, count];
                                    m_dMoveStdAngDownload[3] = 0;
                                    MotionController.RobotMoveDownload(m_dMoveStdAngDownload, RecMode_Rov * Recovery.KneePeriodVel[count] / 2);
                                }
                                break;
                            /* 右单边 */
                            case 2:
                                {
                                    m_dMoveStdAngDownload[0] = 0;
                                    m_dMoveStdAngDownload[1] = Recovery.KneePeriodData[1, count];
                                    m_dMoveStdAngDownload[2] = 0;
                                    m_dMoveStdAngDownload[3] = Recovery.KneePeriodData[3, count];
                                    MotionController.RobotMoveDownload(m_dMoveStdAngDownload, RecMode_Rov * Recovery.KneePeriodVel[count] / 2);
                                }
                                break;
                        }
                        #endregion                       
                    }
                }
            }
        }
        #endregion

        /// <summary>
        ///停止训练
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_StopTrain_Click(object sender, EventArgs e)
        {
            if (MotionDownloadThread.IsAlive)
            {
                MotionDownloadThread.Abort();//关闭线程
            }
            MotionController.RobotMoveForbid();

            #region 训练时间显示
            timer_TrainTime.Enabled = false;
            timer_TrainTime.Stop();
            toolStripStatusLabel_TrainTime.Text = "未训练";
            #endregion

            #region 按键状态管理
            UIThread.ChangeEnable(btn_StopTrain, false);//停止训练
            UIThread.ChangeEnable(btn_AllToZero, true);//全部回零
            #endregion
        }

        /// <summary>
        /// 全部回零
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_AllToZero_Click(object sender, EventArgs e)
        {
            MotionController.GotoZero();

            #region 按键状态管理  
            UIThread.ChangeEnable(btn_ServoOff, true);//伺服禁能
            UIThread.ChangeEnable(btn_AllToZero, false);//全部回零
            UIThread.ChangeEnable(btn_JogForm, true);//点动模块
            UIThread.ChangeEnable(btn_EleActuatorUp,true);//电动推杆向上
            UIThread.ChangeEnable(btn_EleActuatorDown,true);//电动推杆向下 
            UIThread.ChangeEnable(btn_NormalStep, true);//标准步态训练模式
            UIThread.ChangeEnable(btn_SmallArc,  true);//小圆弧训练模式
            UIThread.ChangeEnable(btn_Linear, true);//直线训练模式
            UIThread.ChangeEnable(btn_Oval, true);//椭圆训练模式
            UIThread.ChangeEnable(btn_Circular, true);//圆周训练模式
            UIThread.ChangeEnable(btn_Hip, true);//髋关节训练模式
            UIThread.ChangeEnable(btn_Knee, true);//膝关节训练模式
            UIThread.ChangeEnable(radioButton1, true);//单边/双边模式选择
            UIThread.ChangeEnable(radioButton2, true);
            UIThread.ChangeEnable(radioButton3, true);
            #endregion
        }

        /// <summary>
        ///关闭窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("是否关闭窗口", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                e.Cancel = true;
        }

        /// <summary>
        /// 让label_RecVel的值随trackBar_RecVel变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void trackBar_RecVel_ValueChanged(object sender, EventArgs e)
        {
            label_RecVel.Text = UIThread.ReadTrackBarValue(trackBar_RecVel).ToString();
        }

        #region 电动推杆
        private void btn_EleActuatorUp_MouseDown(object sender, MouseEventArgs e)
        {
            MotionController.ElePutter_UP();
        }

        private void btn_EleActuatorUp_MouseUp(object sender, MouseEventArgs e)
        {
            MotionController.ElePutter_Stop();
        }

        private void btn_EleActuatorDown_MouseDown(object sender, MouseEventArgs e)
        {
            MotionController.ElePutter_Down();
        }

        private void btn_EleActuatorDown_MouseUp(object sender, MouseEventArgs e)
        {
            MotionController.ElePutter_Stop();
        }
        #endregion

        private void timer_TrainTime_Tick(object sender, EventArgs e)
        {
            TimeSpan timeSpan = DateTime.Now - startTime;
            toolStripStatusLabel_TrainTime.Text = "训练时间：" + timeSpan.Hours.ToString() + "小时" +
                timeSpan.Minutes.ToString() + "分钟" + timeSpan.Seconds.ToString() + "秒";
        }    
    }
}
